package com.newmainsoftech.aspectjutil.eventmanager;

import java.lang.reflect.Method;

import junit.framework.Assert;

import org.junit.Test;

public class EventHandlerTestWithEventProducerWrapperAspect extends EventHandlerTestTemplate {
	// Test whether aspect instance can be event listener -----------------------------------------
	@Test
	public void testAspectTypeEventListener() throws Throwable {
		// Execute EventProducerWrapper.calculate method ------------------------------------------
		Object eventProducerWrapperObj 
		= EventManagerTestUtils.eventProducerWrapperFactory( childDelegationClassLoader);
		Method calculateMethod 
		= EventManagerTestUtils.getEventProducerWrapperCalculateMethod( eventProducerWrapperObj.getClass());
		Integer calculationResult = (Integer)calculateMethod.invoke( eventProducerWrapperObj, 4, 8);
		// ----------------------------------------------------------------------------------------
		
		Class<?> eventProducerWrapperAspectClass 
		= EventManagerTestUtils.getEventProducerWrapperAspectClass( childDelegationClassLoader);
		Method getEventInfoMethod
		= EventManagerTestUtils.getEventProducerWrapperAspectGetEventInfoMethod( 
				eventProducerWrapperAspectClass);
		Object eventInfoObj = getEventInfoMethod.invoke( null, new Object[]{});
		Assert.assertNotNull( eventInfoObj);
		
		Class<?> calculationEventClass 
		= EventManagerTestUtils.getCalculationEventClass( childDelegationClassLoader);
		Method getTriggeredEventMethod 
		= EventManagerTestUtils.getEventInfoGetTriggeredEventMethod( eventInfoObj.getClass());
		Assert.assertEquals( 
				calculationEventClass,
				getTriggeredEventMethod.invoke( eventInfoObj, new Object[]{})
				);
		
		Method getEventerInfoMethod 
		= EventManagerTestUtils.getEventInfoGetEventerInfoMethod( eventInfoObj.getClass());
		Object eventerInfoObj = getEventerInfoMethod.invoke( eventInfoObj, new Object[]{});
		Assert.assertEquals( 
				EventManagerTestUtils.getEventerType( "Method", childDelegationClassLoader), 
				EventManagerTestUtils.getEventerTypeOfEventerInfo( eventerInfoObj)
				);
		
		Assert.assertEquals( 
				EventManagerTestUtils.getEventProducerClass( childDelegationClassLoader),
				EventManagerTestUtils.getEventerObjOfEventerInfo( eventerInfoObj).getClass()
				);
		
		Assert.assertEquals( 
				"calculate", 
				EventManagerTestUtils.getEventerMethodOfEventerInfo( eventerInfoObj).getName()
				);
	}
	// --------------------------------------------------------------------------------------------
}
